﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class ActivityHistoryControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        public void LoadPropertyActivityHistory(int companyid, int propertyid, int leaseid)
        {
            if (leaseid == 0)
            {
                pnlAddActivity.Visible = true;
            }
            hfLeaseID.Value = leaseid.ToString();
            hfPropertyID.Value = propertyid.ToString();
            LoadActivityCode(companyid, propertyid, leaseid);
        }

        protected void ddlHaveCost_SelectedIndexChanged(object sender, EventArgs e)
        {
            string havecost = ddlHaveCost.SelectedValue;
            txtActivityCost.Visible = false;
            if (havecost == "Yes")
            {
                txtActivityCost.Visible = true;
            }
        }

        //Begin Activity History

        private bool LoadActivityCode(int companyid, int propertyid, int leaseid)
        {
            bool bHasValue = false;
            if (companyid > 0)
            {
                LoadActivityCodeDDL(companyid);
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                String sqlSELECT = "EXEC rs_uspGetActivityCode @companyid, @propertyid, @leaseid";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@propertyid";
                param.Value = propertyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@leaseid";
                param.Value = leaseid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvActivityCode.DataSource = ds;
                gvActivityCode.DataBind();
                myConnection.Close();

                if (gvActivityCode.Rows.Count > 0)
                {
                    bHasValue = true;
                }
            }
            return bHasValue;
        }

        private DataSet GetChildListDataSet(int companyid, int propertyid, int leaseid, int activitycodeid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            RSUserIdentity userIdentity = GetRSUserIdentity();
            String sqlSELECT = "exec rs_uspGetActivityHistory @companyid, @propertyid, @leaseid, @activitycodeid";
 
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@propertyid";
            param.Value = propertyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@leaseid";
            param.Value = leaseid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@activitycodeid";
            param.Value = activitycodeid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }
        protected void gvActivityCode_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gv = (GridView)e.Row.FindControl("gvActivityHistory");
                if (null != gv && gv is GridView)
                {
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    int activitycodeid = Convert.ToInt32(drv["CodeID"]);
                    int propertyid = int.Parse(hfPropertyID.Value);
                    int leaseid = int.Parse(hfLeaseID.Value);
                    gv.DataSource = GetChildListDataSet(companyid, propertyid, leaseid, activitycodeid);
                    gv.DataBind();
                }
            }
        }
        private void LoadActivityCodeDDL(int companyid)
        {
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "SELECT * FROM rs_tblCode WHERE CompanyId=@companyid AND Category='ACTIVITY_CAT' ORDER BY CodeName ASC";

            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            SqlParameter param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ddlActivityCode.Items.Clear();
            ddlActivityCode.DataSource = myDataReader;
            ddlActivityCode.DataTextField = "CodeName";
            ddlActivityCode.DataValueField = "CodeID";
            ddlActivityCode.DataBind();
            ddlActivityCode.Items.Insert(0, new ListItem("< Please select >", "0"));
            myConnection.Close();
        }
        protected void ddlActivityCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlActivityCode.SelectedValue != "0")
            {
                txtActivityData.BackColor = System.Drawing.Color.White;
                txtActivityDate.BackColor = System.Drawing.Color.White;
                txtActivityData.ReadOnly = false;
                btnAddActivityHistory.Enabled = true;
            }
            else
            {
                txtActivityData.BackColor = System.Drawing.Color.Gainsboro;
                txtActivityDate.BackColor = System.Drawing.Color.Gainsboro;
                txtActivityData.ReadOnly = true;
                btnAddActivityHistory.Enabled = false;
            }
        }
        protected void btnAddActivityHistory_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int propertyid = int.Parse(hfPropertyID.Value);
                int activitycodeid = int.Parse(ddlActivityCode.SelectedValue);
                string activitydata = txtActivityData.Text;
                string activitycost = txtActivityCost.Text;
                string activitydate = txtActivityDate.Text;

                if (activitydata.Length > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ActivityCodeID", SqlDbType.Int, 0, ParameterDirection.Input, activitycodeid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ActivityData", SqlDbType.NVarChar, 250, ParameterDirection.Input, activitydata);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ActivityCost", SqlDbType.Float, 0, ParameterDirection.Input, float.Parse(activitycost));
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ActivityDate", SqlDbType.DateTime, 0, ParameterDirection.Input, Convert.ToDateTime(activitydate));
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@Companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 15, ParameterDirection.Input, userIdentity.RSCurrentUser);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveActivityHistory");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);

                    ddlHaveCost.SelectedValue = "No";
                    txtActivityCost.Text = "";
                    txtActivityData.Text = "";
                    txtActivityDate.Text = "";
                    ddlActivityCode.SelectedValue = "0";
                    btnAddActivityHistory.Enabled = false;
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void gvActivityHistory_Remove(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int activityhistoryid = Convert.ToInt32(e.CommandArgument.ToString());
            if (activityhistoryid > 0)
            {
                int propertyid = int.Parse(hfPropertyID.Value);
                int leaseid = int.Parse(hfLeaseID.Value);
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ActivityHistoryID", SqlDbType.Int, 0, ParameterDirection.Input, activityhistoryid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 15, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblActivityHistory WHERE ActivityHistoryID=@ActivityHistoryID AND CompanyID=@CompanyID");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                LoadPropertyActivityHistory(userIdentity.RSCompanyId, propertyid, leaseid);
            }
        }

        //End of ActivityHistory
        private string RedirectToCurrentURL(string activetab)
        {
            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", activetab);
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            return url;
        }
    }
}